import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		auditStatus: [{ name:'已作废', value:-3 }],
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '医院名称',		name: 'hosp_id',				type:'select',		loading:true },
			{ label: '操作人',		name: 'operator_id',			type:'select',		loading:true },
			{ label: '作废时间',		names: ['start_at','end_at'], 	type:'date-range', 	loading:true },
		],
	}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			$fn.setRow( forms, 'hosp_id', await f.getCache({ api:'hospital' }) )
			// 操作人
			$fn.setRow( forms, 'operator_id', await f.getCache({ api:'employee' }) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/invalidlists')
	// 表单
	cols = [
		{ title: '标本箱编号', 		dataIndex: 'box_num', 		...$fn.table.spec_code },
		{ title: '标本箱条码号', 	dataIndex: 'box_code', 		...$fn.table.spec_code },
		{ title: '医院名称', 		dataIndex: 'hosp_name', 	width:180 },
		{ title: '条码号', 			dataIndex: 'spec_code', 	...$fn.table.spec_code },
		{ title: '项目列表', 		dataIndex: 'kind_str', 		width:200 },
		{ title: '录入时间', 		dataIndex: 'created_at',  	...$fn.table.time},
		{ title: '作废时间', 		dataIndex: 'operator_at',  	...$fn.table.time},
		{ title: '作废原因', 		dataIndex: 'reason', 		width:200 },
		{ title: '操作员', 			dataIndex: 'operator_name', ...$fn.table.name },
		{ title: '项目状态', 		dataIndex: 'audit_status', 	width:100,	render: text => $fn.getState(this.state.auditStatus,text) },
	]
	
	// 操作按钮
	controls = () => {
		return [
			Confirm.call(this, '恢复作废条码', 'specimen/recover', 	{ isKeys:1, query:( keys,rows )=>{return {spec_code:rows.map( v => v.spec_code )}}}),
			// 增加导出 2021-9-27  --马婷
			Controls.exports.call(this,'specimen/invalidlists',{label:'导出'})	// 导出
		]
	}
		
	render(){
		return (
			<Page title='标本作废记录'  controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this, {isInit: window.isInit}) }
			</Page>
		)
	}
}

export default Index